package br.com.intelsys.security.itoken;

public abstract class ITokenLocalServiceAbstract {
	private final ITokenService iTokenService;

	public ITokenLocalServiceAbstract(ITokenService iTokenService) {
		this.iTokenService = iTokenService;
	}

	protected IToken useToken(String token, boolean useToken) throws ITokenNotFoundException, ITokenCodAuxNotMatchException,
			ITokenViolationException {
		TokenEncoder tokenEncoder = iTokenService.getDecodedToken(token);
		if (useToken) {
			return iTokenService.useToken(tokenEncoder.getToken(), tokenEncoder.getCodAux());
		} else {
			return iTokenService.getTokenWithoutUse(tokenEncoder.getToken(), tokenEncoder.getCodAux());
		}
	}

	protected IToken createPasswordRecoverToken(int codITokenType, int codUsr) {
		return iTokenService.createUniqueIToken(codITokenType, codUsr, 0, 1, 3);
	}

	protected IToken createKeepLoggedToken(int codITokenType, int codUsr) {
		return iTokenService.createUniqueIToken(codITokenType, codUsr, 0, 0, 90);
	}

	public void revokeToken(String token) {
		try {
			iTokenService.revokeEncodedToken(token);
		} catch (ITokenNotFoundException e) {
			e.printStackTrace();
		}
	}

}
